package java44th.group.controller.wx.auth;


import java44th.group.bean.admin.vo.BaseRespVo;

import java44th.group.bean.po.MarketUser;
import java44th.group.bean.wx.bo.auth.LoginAuthData;

import java44th.group.bean.wx.bo.auth.UserInfo;
import java44th.group.config.realm.MarketToken;
import java44th.group.service.wx.auth.WxAuthService;
import java44th.group.utils.MessageUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @Aurhor: 徐云飞
 * @DATE: 2022/10/14 9:36
 * @Description:
 * @Version 1.0
 */

@RestController
@RequestMapping("wx/auth")
public class WxAuthController {

    @Autowired
    WxAuthService wxauthService;


    //登录
    @PostMapping("login")
    public BaseRespVo login(@RequestBody Map map) {
        String username = (String) map.get("username");
        String password = (String) map.get("password");

        // 写业务
        Subject subject = SecurityUtils.getSubject();

        try {
            subject.login(new MarketToken(username, password, "user"));
        } catch (AuthenticationException e) {
            e.printStackTrace();
        }

        if (subject.isAuthenticated()) {

        }

        Session session = subject.getSession();

        MarketUser primaryPrincipal = (MarketUser) subject.getPrincipals().getPrimaryPrincipal();
        LoginAuthData loginAuthData = new LoginAuthData();
        UserInfo userInfo = new UserInfo();
        userInfo.setAvatarUrl(primaryPrincipal.getAvatar());
        userInfo.setNickName(primaryPrincipal.getNickname());
        loginAuthData.setUserInfo(userInfo);

        loginAuthData.setToken((String) session.getId());

        return BaseRespVo.ok(loginAuthData);
    }

    //登出
    @PostMapping("logout")

    public BaseRespVo logout() {
        Subject subject = SecurityUtils.getSubject();
        subject.logout();

        return BaseRespVo.ok();

    }

    //注册账号
    @PostMapping("register")
    public BaseRespVo register(@RequestBody Map map) {
        String mobile = (String) map.get("mobile");

        if (mobile.length() != 11 || !StringUtils.isNumeric(mobile)) {

            return BaseRespVo.error(707, "手机号格式不对");

        }

        if (Message(map, mobile)) return BaseRespVo.error(703, "验证码错误");
        int register = wxauthService.register(map);

        if (register == 1) {
            return BaseRespVo.error(704, "用户名重复");

        }

        if (register == 2) {
            return BaseRespVo.error(705, "手机号已注册");
        }

        Map<String, String> map1 = new HashMap<>();
        map1.put("username", (String) map.get("username"));
        map1.put("password", (String) map.get("password"));
        BaseRespVo login = login(map1);
        return login;
    }

    private boolean Message(@RequestBody Map map, String mobile) {
        String ssm = MessageUtils.messageMap.get(mobile);
        // String ssm = (String) session.getAttribute("ssm");
        String code = (String) map.get("code");

        if (!code.equals(ssm)) {

            return true;

        } else {

            MessageUtils.messageMap.put(mobile, null);

        }
        return false;
    }


    //重置密码
    @PostMapping("reset")
    public BaseRespVo reset(@RequestBody Map map) {
        String mobile = (String) map.get("mobile");
        if (mobile.length() != 11 || !StringUtils.isNumeric(mobile)) {

            return BaseRespVo.error(402, "参数值不对");

        }

        if (Message(map, mobile)) return BaseRespVo.error(703, "验证码错误");

        wxauthService.reset(map);

        return BaseRespVo.ok();
    }


    //发送验证码
    @PostMapping("regCaptcha")
    public BaseRespVo regCaptcha(@RequestBody Map map) {

        String mobile = (String) map.get("mobile");

        if (mobile.length() != 11 || !StringUtils.isNumeric(mobile)) {

            return BaseRespVo.error(402, "手机号格式不对");

        }

        String s = wxauthService.regCaptcha(map);

        MessageUtils.messageMap.put(mobile, s);

        return BaseRespVo.ok();
    }


}